/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoaderPanel.java
 *
 * Created on Apr 22, 2012, 2:23:19 PM
 */
package pacificlms;

import java.awt.event.ActionListener;
import javax.swing.Timer;

import pacificlms.MainPanel.MainPanel;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.*;
import java.awt.event.*;


import java.sql.*;
import java.util.*;
import java.text.*;


import pacificlms.Verifiable.*;
import pacificlms.Verifiable.Database.*;

import java.net.*;
import java.io.*;
import pacificlms.Error.ErrorHandle;


/**
 *
 * @author brianhhh
 */
public class LoaderPanel extends javax.swing.JPanel {

    /** Creates new form LoaderPanel */
    public LoaderPanel(String ConnectionURL) {
        initComponents();
        
        LoadingDisplayTimer = new Timer(100, TimerLoadDisplay);
        LoadingDisplayTimer.start();
                
        ConnURL = ConnectionURL;
        Thread Loader = new Thread(new Runnable() { public void run() { LoadTimer(ConnURL); } } );
        Loader.start();
    }
    
    final String ConnURL;
    public boolean Done = false;
    
    void DisplayTimer()
    {
        ProgressBar.setValue((ProgressBar.getValue() + 1) % 100);
        if (Done)
        {
            this.setVisible(false);
            MainJApplet.getApplet().createMainGUI();
            LoadingDisplayTimer.stop();
            this.getParent().remove(this);
        }
    }
    
    void LoadTimer(String ConnectionURL)
    {

        Connection WorkingConnection = null;
        while (WorkingConnection == null)
        {
            try
            {
                //fixme switch to ConnectionURL from "http://www.humenet.net/cics505/Pacific%20Team/ConList.cfg"
                //ConnectionURL = "http://www.humenet.net/cics505/Pacific%20Team/ConList.cfg";
                ArrayList<ConnectionInfo> ConnectionInfoList = ConnectionInfo.LoadListFromURL(ConnectionURL);

                ConnectionInfo SelectedConnectionInfo = null;
                if (ConnectionInfoList.size() == 1)
                {
                    SelectedConnectionInfo = ConnectionInfoList.get(0);
                }
                else
                {
                    SelectConnection SC = new SelectConnection(ConnectionInfoList, true);       
                    SC.setVisible(true);
                    if (SC.ForceExit)
                    {
                        this.setVisible(false);
                        return;
                    }
                    SelectedConnectionInfo = SC.GetSelectedConnection();
                }

                DriverManager.registerDriver(new com.mysql.jdbc.Driver());
                Class.forName("com.mysql.jdbc.Driver");

                WorkingConnection = SelectedConnectionInfo.CreateConnection();
                
                if (!DataHolder.LoadData(WorkingConnection))
                {
                    WorkingConnection = null;
                }
            }
            catch(Exception Ex)
            {
                ErrorHandle.Display("Connection Error", "Connection error\nPlease close the LMS and try running it again\n\nError:\n" + Ex.toString(), Ex, true);
                WorkingConnection = null;
            }
        }
        
        MasterConnection.Initialize(WorkingConnection);
        LoggedMember.Clear();
        StartupTestArea.RunTest();
        
        Done = true;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ProgressBar = new javax.swing.JProgressBar();
        lblLoading = new javax.swing.JLabel();

        lblLoading.setFont(new java.awt.Font("Tahoma", 0, 22)); // NOI18N
        lblLoading.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblLoading.setText("Pacific LMS is Loading...");
        lblLoading.setToolTipText("");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblLoading, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
                    .addComponent(ProgressBar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(268, 268, 268)
                .addComponent(lblLoading)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(267, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JProgressBar ProgressBar;
    private javax.swing.JLabel lblLoading;
    // End of variables declaration//GEN-END:variables
    private ActionListener TimerLoadDisplay = new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DisplayTimer();
            }
        };
    private javax.swing.Timer LoadingDisplayTimer;
}
